/*
 * Created by SharpDevelop.
 * user: Robertux
 * Date: 20/03/2008
 * Time: 11:20
 * 
 * 
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using DataLayer;
using Inventador;

namespace WinApp
{
	/// <summary>
	/// Description of frmEntradasSalidas.
	/// </summary>
	public partial class frmEntradasSalidas : Form
	{
		private BaseDatos bdatos;
		private List<Movimiento> movimientos;
		private DataGridViewCellStyle estiloEntrada;
		private DataGridViewCellStyle estiloSalida;
		
		public frmEntradasSalidas()
		{
			InitializeComponent();
		}
		
		public frmEntradasSalidas(BaseDatos bdatosExterna, string tipoMov): this()
		{
			this.bdatos = bdatosExterna;
			this.movimientos = Movimiento.GetList(this.bdatos, "id != 0", "Fecha DESC");			
			this.comboBox1.SelectedIndex = 2;
			if(tipoMov == "Entrada")
				this.comboBox1.SelectedIndex = 0;
			if(tipoMov == "Salida")
				this.comboBox1.SelectedIndex = 1;
			if(tipoMov == "")
				this.comboBox1.SelectedIndex = 2;
			this.estiloEntrada = new DataGridViewCellStyle();
			this.estiloSalida = new DataGridViewCellStyle();
			this.estiloEntrada.ForeColor = Color.Green;
			this.estiloSalida.ForeColor = Color.DarkRed;
			this.RellenarGrid();
			this.gridManager1.AutoGenerarColumnas();
			this.RellenarCombosFecha();
		}
		
		public void RellenarGrid()
		{
			this.grdMovimientos.Rows.Clear();
			for(int i = 0; i < this.movimientos.Count; i++)
			{
				if(((this.comboBox1.SelectedIndex == 0 || this.comboBox1.SelectedIndex == 2) && (this.movimientos[i].TipoMovimiento == "Entrada")) || ((this.comboBox1.SelectedIndex == 1 || this.comboBox1.SelectedIndex == 2) && (this.movimientos[i].TipoMovimiento == "Salida")))
				{
					if((this.movimientos[i].Fecha.Year.ToString() == this.comboBox2.Text) && (this.movimientos[i].Fecha.Month == (this.comboBox3.SelectedIndex + 1)))
					{
						this.grdMovimientos.Rows.Add(new object[]{this.movimientos[i].Id, this.movimientos[i].Fecha.ToShortDateString(), this.movimientos[i].TipoMovimiento, this.movimientos[i].fkProducto.fkTipoProducto.Nombre + " " + this.movimientos[i].fkProducto.fkMarcaProducto.Nombre, this.movimientos[i].Cantidad});
						this.grdMovimientos.Rows[this.grdMovimientos.Rows.Count - 1].Cells[2].Style = (this.movimientos[i].TipoMovimiento == "Entrada"? this.estiloEntrada: this.estiloSalida);
					}
				}					
			}
		}
		
		public void RellenarCombosFecha()
		{
			if(this.movimientos.Count > 0)
			{
				this.comboBox3.SelectedIndexChanged -= new System.EventHandler(this.ComboBox3SelectedIndexChanged);
				this.comboBox2.SelectedIndexChanged -= new System.EventHandler(this.ComboBox2SelectedIndexChanged);
				this.comboBox3.Enabled = true;
				this.comboBox2.Enabled = true;
				int minYear = this.movimientos[0].Fecha.Year;
				this.comboBox2.Items.Clear();
				for(int i = minYear; i <= DateTime.Now.Year; i++)
					this.comboBox2.Items.Add(i.ToString());
				this.comboBox2.SelectedIndex = this.comboBox2.Items.Count - 1;
				this.comboBox3.SelectedIndex = DateTime.Now.Month - 1;
				this.comboBox3.SelectedIndexChanged += new System.EventHandler(this.ComboBox3SelectedIndexChanged);
				this.comboBox2.SelectedIndexChanged += new System.EventHandler(this.ComboBox2SelectedIndexChanged);
				this.RellenarGrid();
//				this.ActualizarDetalles();
//				this.ActualizarValoresGrafico();
			}
			else
			{
				this.comboBox3.Enabled = false;
				this.comboBox2.Enabled = false;
			}
		}
		
		void Button1Click(object sender, EventArgs e)
		{
			this.Close();
		}
		
		void ComboBox1SelectedIndexChanged(object sender, EventArgs e)
		{
			this.RellenarGrid();
		}
		
		void Button2Click(object sender, EventArgs e)
		{
			frmAddMovimiento frmaddmov = new frmAddMovimiento(this.bdatos);
			frmaddmov.ShowDialog(this);
			if(frmaddmov.Result == DialogResult.OK)
			{
				this.movimientos = Movimiento.GetList(this.bdatos, "id != 0", "");
				this.RellenarGrid();
			}
			frmaddmov.Dispose();
		}
		
		void ComboBox2SelectedIndexChanged(object sender, EventArgs e)
		{
			this.RellenarGrid();
		}
		
		void ComboBox3SelectedIndexChanged(object sender, EventArgs e)
		{
			this.RellenarGrid();
		}
	}
}
